package cn.ynu.cmrs.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import cn.ynu.cmrs.R;
import cn.ynu.cmrs.model.CommentItem;
import cn.ynu.cmrs.widget.RemoteImageView;

/**
 * Adapter representing comments
 * 
 * @author Yasha
 * 
 */
public class CommentAdapter extends ArrayListAdapter<CommentItem> {

	public CommentAdapter(Activity context) {
		super(context);
	}

	public CommentAdapter(Activity context, ArrayList<CommentItem> list,
			ListView listView) {
		super(context);
		this.mList = list;
		this.mListView = listView;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		if (position == getCount() - 1) {
			return -1;
		} else {
			return Long.parseLong(mList.get(position).getCommentID());
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		View row;
		if(convertView == null){
			
		}
		if (position == this.getCount() - 1)// see more
		{
			TextView tv;
			row = LayoutInflater.from(mContext).inflate(R.layout.moreitemsview,
					null);
			tv = (TextView) row.findViewById(R.id.tvItemContent);
			tv.setText(R.string.lookMore);
			return row;
		} else {
			
			LayoutInflater inflater = mContext.getLayoutInflater();
			row = inflater.inflate(R.layout.comment_item, null);
			holder = new ViewHolder();
			holder.icon = (RemoteImageView) row
					.findViewById(R.id.ivItemPortrait);
			holder.comment = (TextView) row.findViewById(R.id.tvItemSubContent);
			holder.commTime = (TextView) row.findViewById(R.id.tvItemDate);
			holder.nickName = (TextView) row.findViewById(R.id.tvItemName);
			holder.mark = (RatingBar) row.findViewById(R.id.markRatingbar);
			holder.icon.setDefaultImage(R.drawable.portrait);
			holder.icon.setImageUrl(mList.get(position).getUserIcon(),
					position, getListView());
			holder.nickName.setText(mList.get(position).getUserName());
			holder.comment.setText(mList.get(position).getComment());
			holder.commTime.setText(mList.get(position).getTime());
			holder.mark.setRating((float) (mList.get(position).getMark()) / 2);
			return row;
		}
	}

	/**
	 * Add more data to adapter
	 * 
	 * @param items
	 */
	public void addMoreData(ArrayList<CommentItem> items) {
		this.mList.addAll(items);
		this.notifyDataSetChanged();
	}

	/**
	 * Class implementing holder pattern, performance comment
	 * 
	 * @author Yasha
	 *
	 */
	static class ViewHolder {
		RemoteImageView icon;
		TextView nickName;
		TextView commTime;
		TextView comment;
		RatingBar mark;
	}

}
